Central Article

Using the Central Accordion Tab Component

Mike Chambers

Developer Relations Product Manager, Macromedia

One of the most popular components included with the Macromedia Central SDK is the Accordion Tab component. This component provides a stylized tab-based control that animates the tabs between states.

This article covers a number of techniques that show how to use the Accordion Tab component, including:

  • Adding tabs to the component.
  • Detecting when the selected tab has changed.
  • Using the component to manage the state of multiple views or clips.
  • Using the component without tabs in order to provide user interface layout boundaries.
  • Adding support for animating the favorite icon.

Requirements

To complete this tutorial you need to install the following software and files:

Macromedia Central
Macromedia Central SDK

Macromedia Flash MX 2004


Download the sample files


Getting Started

Before you get started, you have to first make sure that the component is installed. If you have already installed the component, then you may skip this step.

  1. Download the Macromedia Central SDK. The SDK contains Central documentation, resources, samples as well as the components.
  2. Install the Central components. These can be found in the Components/ directory of the SDK. Follow the directions in the readme.txt file located in that directory.
  3. Open Macromedia Flash (or restart it if it was open).
  4. Open the Components Panel (Window > Development Panels > Components).
  5. Expand the Central Components Section within the Components Panel.
  6. The Accordion Tab component is the first component (listed as MAccordionTab as shown in Figure 1).

The Accordion Tab component in the Components panel

Figure 1. The Accordion Tab component in the Components panel

There are a couple of items to keep in mind when placing an instance of the According Tab component into your application within the Flash authoring environment:

  • The runtime size of the component is determined by its author time size.
  • Live preview for most of the Central components, including the Accordion Tab, is not supported.
  • The Central components will not work when testing within the Flash environment. Therefore, you must test them within Central.

Adding Tabs to the Accordion Tab Component

The Accordion Tab API contains three methods for adding data and tabs to the component:

  • MAccordionTab.addItem
  • MAccordionTab.addItemAt
  • MAccordionTab.setDataProvider

As you have probably figured out by looking at the method above, the component supports the DataProvider API.

Each tab can have a label (which is required) and optional data associated with it. Adding a tab to the component is as simple as calling addItem, and passing a label to the method.

Adding a Tab Using addItem

The easiest way to add a tab to the component is to use the addItem method, which appends a tab with the specified label to the component.

Note that in order to test the component, you will have to install and run the test application within Central. I will not cover the steps on how to do that here. If you are not sure how to install an application into Central, please read the article Getting Started with Macromedia Central. You can use the test file created in that article for the examples in this article. This article assumes that you are testing your application within Central.

Adding a single tab is as easy as dragging the Accordion Tab component into your application, giving it an instance name ("myTab") and adding the following ActionScript code:

function onActivate()
{
     myTab.addItem("Tab 1");
}

Central.initApplication(this, this);

However, if you test your movie, you will not see any tabs but a bevel background with a title “Tab 1” (see Figure 2).

What you see when testing your movie

Figure 2. What you see when testing your movie

This is because if you add only one item to the component instance, it does not appear as a colored tab but as a title in the content area. You need a minimum of two items to have differentiated tabs visible to the user.

You can add multiple tabs by calling addItem multiple times.

function onActivate()
{
     myTab.additem("Tab 1");
     myTab.additem("Tab 2");
     myTab.additem("Tab 3");
}

Central.initApplication(this, this);

This creates a tab control with three tabs (see Figure 3).

A tab control with three tabs

Figure 3. A tab control with three tabs

If you look at the SDK documentation for the addItem method, you will notice that it can take two parameters. The second parameter is data that will be associated with the tab. This data can be any ActionScript data type including an object, array, string, or even a reference to a movie clip. This last item will be useful if you want to show how to change application views based on the user’s interaction with the tabs.

Adding a Tab Using setDataProvider

Since the Tab component supports the DataProvider API, you can also use a Central.DataProviderClass to populate the tab and its data. The component recognizes the following data provider items and properties:

  • label specifies the label of the tab.
  • data specifies the data associated with the tab.
  • icon enables you to add a favorites icon to the tab.

As is the case with the addItem method, only the label property is required; the other properties are optional. The following snippet shows how to use setDataProvider to add three tabs to the component:

function onActivate()
{
    var dp = new Central.DataProviderClass();
        dp.addItem({label:”Tab 1”});
        dp.addItem({label:”Tab 2”});
        dp.addItem({label:”Tab 3”});

    myTab.setDataProvider(dp);
}

Central.initApplication(this, this);

The result resembles the tab control created using the addItem method (see Figure 3).

Determining When the User Selects a Tab

Now that you know how to add tabs to the component, the next step is to figure out how to determine when the user selects a tab, and which tab she selected.

In order to determine when the user selects a tab, you need to implement a change handler function. This function is called whenever the selected index of the component changes (in most cases this occurs when the users selects a tab). You can specify a change handler for your component using the setChangeHandler method.

The setChangeHandler method of the Accordion Tab component takes the name of the function that is called when the selected index of the component changes. It will be called in the same scope as the component (that is the same timeline). If the function exists in a different scope than the component, you can pass in a reference to that scope as the second argument.

The following example demonstrates how to add a change handler to the addItem example from above.

function onActivate()
{
     myTab.additem("Tab 1");
     myTab.additem("Tab 2");
     myTab.additem("Tab 3");

     myTab.setChangeHandler("onTabChange");
}

function onTabChange()
{
     var tabLabel = myTab.getSelectedItem().label;
     trace("Tab Selected : " + tabLabel);
}

Central.initApplication(this, this);

First use the setChangeHandler component to specify that the onTabChange function be called when the selected tab changes.

Then define the onTabChange function. All you do here is get a reference to the currently selected tab (by calling getSelectedItem), and trace out its label.

In order to test it, open the Central Debug Panel (Window > Other Panels > Central Debug Panel) and then publish the application into Central. Switch to Central, reload the test application and then select a tab. If you look at the debug panel, you should see the trace information for each tab selected.

You can now add code that changes the state of your application when a new tab is selected.

Switching Between Views When the Selected Tab Changes

Because tabs provide an interface for multiple related items, tabbed controls lend themselves to applications that contain multiple views. The tabs can be used to manage the state and presentation of the views.

This section provides a simple example that shows:

  1. How to store references to a view or movie clip within each tab.
  2. How to use the tab control to manage the state of the possible views.

I'll show you how to create a simple example that toggles the state of three simple movie clips. However, the example can easily be applied to managing more complex movie clips that have classes associated with them.

First you need to create the views or movie clips whose state will be controlled by the Accordion Tab component. Then write the necessary ActionScript code.

The article assumes that you are starting with the file you created in the addItem section. If not, just create a new movie with two layers named “Actions” and “accordion”. Drag an instance of the Accordion Tab component onto the accordion layer, and give it an instance name of “myTab”.

  1. Create three movie clips, and name them “red”, “green”, and “blue”.
  2. Open each one and draw a 100 x 100 square. The color of the square should match the clip’s name (that is red, green, or blue).
  3. Make sure that each square is at coordinate 0,0.
  4. Create three new layers on your timeline (above the accordion layer) and name them “red”, “green” and “blue”. If you like, you can place these within a folder to help organize them.
  5. Drag an instance of each clip onto its respective timeline.
  6. Give each clip an instance name that matches its symbol name (i.e. “red”, “green”, or “blue”).
  7. Finally, position each clip in the bottom left of the accordion tab and make sure that the x,y coordinates of each clip are exactly the same.

At this point, your timeline and stage should resemble Figure 4.

Timeline and Stage with view clips

Figure 4. Timeline and Stage with view clips

Now you can add the ActionScript that manages the application. I'll give you the code first, followed by a detailed discussion of the script.

function onActivate()
{
     green._visible = false;
     blue._visible = false;

     myTab.addItem("red", red);
     myTab.addItem("green", green);
     myTab.addItem("blue", blue);

     selectedTab = red;

     myTab.setChangeHandler("onTabChange");
}

function onTabChange()
{
     selectedTab._visible = false;

     var tab = myTab.getSelectedItem().data;

     tab._visible = true;
     selectedTab = tab;
}

Central.initApplication(this, this);

Basically, you make all of the clips that are not selected invisible (_visible = false). You then store a reference to each clip in its respective tab, and when a tab is selected, you toggle the visibility of the clips.

The item selected by default is the red clip. So you make the green and blue clips invisible:

     green._visible = false;
     blue._visible = false;

Next add the tabs to the component using the addItem method.

     myTab.addItem("red", red);
     myTab.addItem("green", green);
     myTab.addItem("blue", blue);

Notice that the data stored with each tab (the second parameter) is actually a reference to the tab’s respective clip.

You can also use the setDataProvider method, in which case the reference to the clip is stored in the data property.

Next create a new property called selectedTab. This property stores a reference to the currently selected (and thus visible) tab.

     selectedTab = red;

Finally, set the change handler for the component to the onTabChange function.

The onTabChange function gets the clip reference from the selected tab, and then based on that toggles the state of the clips.

First, it sets the selectedTab’s visibility to false.

     selectedTab._visible = false;

Then, it provides a reference to the clip from the newly selected tab. Make that clip visible, and then set it as the selected tab:

     var tab = myTab.getSelectedItem().data;
	 
     tab._visible = true;
     selectedTab = tab;

If you publish the application into Central and test it, the visible clip changes based on the selected tab.

Of course, in most cases the clips would be more complex and have classes associated with them. However, the code for switching between them would be the same.

Instead of toggling the visibility, you can also dynamically attach and remove the movie clips. The code is pretty much the same, except instead of setting the visibility, you attach the clip to be displayed, and remove the previously selected clip. The main advantage to this technique is that all of your clips do not have to be loaded into memory. For applications with many views, or views that encompass a lot of functionality, this can make a significant difference in performance and memory usage.

The following sections outline the pros and cons of each technique.

The Pros and Cons of Setting Visibility

Pros

  • Switching between views is relatively fast.
  • Views automatically maintain state.
  • You can access all views via ActionScript even if they are not currently displayed.
  • Views do not need to initialize when they are switched to.

Cons

  • Requires that all clips be loaded into memory (thus requires more memory).
  • Initial initialization takes longer, since each view has to initialize on load / activate.

The Pros and Cons of Dynamically Attaching and Removing Movie Clips

Pros

  • Uses less memory, because only active views are loaded at any one time.
  • Initial initialization is relatively faster, because only the selected item has to be initialized on load or activate.

Cons

  • Switching between views is relatively slower, because the view must initialize itself.
  • Not as efficient, since views must be initialized every time they are loaded.
  • Views do not automatically maintain their state. State must be explicitly managed via ActionScript.

You can use these pros and cons to help determine the technique best suited for your application.

Using the Accordion Tab Component for Application Layout

The Accordion Tab component creates a beveled box that contains tabs at the top. However, if you do not add any tabs to the component, a beveled box is still created. This can be used as a background graphic to help delineate different sections of your application (see Figure 5).

Use of the Accordion Tab component for tabs and layout in the Active Content Resource application

Figure 5. Use of the Accordion Tab component for tabs and layout in the Active Content Resource application

This technique is as easy as dragging the component into your application and sizing it. This creates a beveled box in the default color.

For this example, go ahead and change the color using the setBaseColor function of the component.

Here is the code for the example:

function onActivate()
{
     frameCount = 0;

     onEnterFrame = function()
     {
          if(frameCount < 1)
          {
               frameCount++;
               return;
          }

          myTab.setBaseColor(0x003366);

          delete frameCount;
          delete onEnterFrame;
     }
}

Central.initApplication(this, this);

This creates within the application the beveled box shown in Figure 6.

Using the Accordion Tab component to create a beveled box

Figure 6. Using the Accordion Tab component to create a beveled box

However, that seems like a lot of code to just set the color. Unfortunately, it is necessary due to an issue with the component. You must wait a frame before you can call methods on it that affects its appearance. The code above basically waits a frame after the component has loaded to call the setBaseColor method.

If you test the example, you can actually see the component change colors, which is not good. The workaround is to set the color at author time using the Property Inspector (see Figure 7).

Setting the Accordion Tab color in the Property Inspector

Figure 7. Setting the Accordion Tab color in the Property Inspector

Adding Favorite Tab Support

If you have viewed many Central applications, you have probably noticed that a few of them have favorite tabs that allow users to store their favorite items for the application. Each favorite tab contains an icon that visually indicates it’s the tab's function.

The application also contains a button icon with the favorites icon. When the user selects the button, the favorites icon moves to the tab to tell the user visually that the item has been added to the user’s favorites.

You can achieve this functionality by combining the MAccordionTab and MIconButton components. This section explains show how to add this animation to your application.

You can use the file created in the setDataProvider section as the basis for this example.

First, you need to make the icon appear on the tab. This is done by specifying the icon property in the data provider for the favorites tab. Note that this will not work when populating the component using the addItem or addItemAt methods. You must populate it using the setDataProvider method.

Here is the code necessary to add the icon to a tab:

function onActivate()
{
    var dp = new Central.DataProviderClass();
        dp.addItem({label:"Tab 1"});
        dp.addItem({label:"Tab 2", icon:"MAccordionTabFavoritesFlag"});
        dp.addItem({label:"Tab 3"});

    myTab.setDataProvider(dp);
}

Central.initApplication(this, this);

Notice you add a property called icon with the value of “MAccordionFavoritesFlag” to the second item:

dp.addItem({label:"Tab 2", icon:"MAccordionTabFavoritesFlag"});

This places a favorites icon on the second tab, labeled “Tab 2”.

Next, you need to place the button onto the Stage that contains the favorites icon.

  1. Create a new layer above the accordion’s layer and name it “icon button”.
  2. Drag an instance of the MIconButton component from the Central section of the components panel to the new layer.
  3. With the component selected, set the Built in Icon property in the property inspector to “icon_bookmark_add”.

That is all that is necessary. Publish the application, and view it within in Central (see Figure 8).

Accordion Tab and IconButton with favorites icons

Figure 8. Accordion Tab and IconButton with favorites icons

The tab and icon button controls should be visible. If you click the icon button, the favorites icon will move from the button to the tab.

There is also an animation for removing an item from favorites. To view it, switch back to Flash, and change the icon for the IconButton component to “icon_button_remove”. Publish and test your application within Central.

Now, when you click the button, the favorites icon in the tab animates to indicate that the item was removed.

The code that actually copies the data from one section to another is beyond the scope of this article.

Additional Resources


About the author

Mike Chambers has been creating applications integrating Macromedia Flash with server-side technologies (Macromedia ColdFusion, Java, ASP, Flash Communication Server, and PHP) for a number of years. He is currently the Product Manager for Developer Relations for Macromedia Central. He has written and spoken extensively on Macromedia Flash and Rich Internet Applications and is co-author of "Flash Enabled: Flash Design and Development for Devices" as well as "Generator and Flash Demystified." Mike received his Masters in International Economics and European Studies from the John Hopkins School of Advanced International Studies (SAIS) in 1998.